package com.kr.redeye.util;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class Mail {

	 public static void main(String[] args)
	 {
		 try {
			 System.out.println(getInstance().sendMail("mail", ""));
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	
	private static Mail instance = new Mail();

    public static Mail getInstance() {
    	//System.out.println("Mail getInstance");
        return instance;
    }

    private Mail() {
    }

    private class SMTPAuthenticator extends Authenticator {
    	PasswordAuthentication passwordAuthentication;
    	SMTPAuthenticator(String userName, String password) {
    		passwordAuthentication = new PasswordAuthentication(userName, password);
    	}
		public PasswordAuthentication getPasswordAuthentication() {
			return passwordAuthentication;
		}
    }

    /**
     * ���� ������

     * @return
     * @throws Exception
     */
    public String sendMail(String toMail, String userName) throws Exception {
    	// name     : 보낼사람 이름
        // from     : 보낼사람 주소
        // to       : 받을사람 주소
        // title    : 제목
        // content  : 내용
        // tar      : text or html
        // filename ; 첨부파일 절대 경로(파일명 포함)

    	String host = "smtp.naver.com";
        String usid = "lalunabella";
        String pswd = "khj20540";
        
        String from = "lalunabella@naver.com";
        String to = toMail;
        
        String content="";
        
        content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
     	content += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
        content += "<head>";
        content += "<title>Smart VE 가입 인증메일</title>";
        content += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        content += "</head>";
        content += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
        content += "<table width=\"530\" height=\"550\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse;\">";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_01.jpg\" width=\"530\" height=\"175\" alt=\"Smart VE 회원가입 인증메일\">";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "		<td width=\"380\" height=\"100\" leftmargin=\"75\" style=\"font-size:12px; line-height:1.6;\">";
        content += "			안녕하세요! " + userName + " 님 !<br />";
        content += "			Smart VE 계정 등록을 요청하셨습니다.<br />";
        content += "			회원가입 및 활성화를 위해서는 이메일 인증이 필요합니다.<br />";
        content += "			아래 '가입완료' 버튼을 클릭해 주세요.";
        content += "		</td>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"180\">";
        //content += "			<a href=\"http://localhost:8080/smartve/mailok.do?action=mailok&id="+toMail+"\"><img src=\"images/auth_04.jpg\" width=\"205\" height=\"50\" alt=\"가입완료\"></a>";
        content += "			<a href=\"http://121.78.122.210:8080/smartve/mailok.do?action=mailok&id="+toMail+"\"><img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_04.jpg\" width=\"205\" height=\"50\" alt=\"가입완료\"></a>";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_07.jpg\" width=\"530\" height=\"95\" alt=\"SMART VE\">";
        content += "		</td>";
        content += "	</tr>";
        content += "</table>";
        content += "</body>";
        content += "</html>";
        
        
        
        String title = "[PMSC Smart VE] 안녕하세요. " + userName +"(" + toMail + ") 님의 SMART VE 회원가입을  환영 합니다.";

    	try{
	    	Properties props = new Properties();
	    	props.put("mail.transport.protocol", "smtp");
	    	props.put("mail.smtp.starttls.enable","true");
	    	props.put("mail.smtp.host", host);
	    	props.put("mail.smtp.auth", "true");
	    	Authenticator authenticator = new SMTPAuthenticator(usid, pswd);

	    	// use mail address from HTML form for from address
	    	props.put("mail.from", from);
	    	Session session = Session.getInstance(props, authenticator);

	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(from));
	    	msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	    	msg.setSubject(title);
	    	msg.setSentDate(new Date());
	    	
	    	// Content is stored in a MIME multi-part message
	    	// with one body part
	    	MimeBodyPart mbp = new MimeBodyPart();
	    	//mbp.setText(content);
	    	mbp.setContent(content, "text/html;charset=utf-8");
	    	Multipart mp = new MimeMultipart();
	    	mp.addBodyPart(mbp);
	    	msg.setContent(mp, "text/html;charset=utf-8");

	    	Transport.send(msg);


        }
        catch(Exception e){
        	e.printStackTrace();
        }

    	return "succ";
    }
    
    public String sendPassMail(String toMail, String userName) throws Exception {
    	// name     : 보낼사람 이름
        // from     : 보낼사람 주소
        // to       : 받을사람 주소
        // title    : 제목
        // content  : 내용
        // tar      : text or html
        // filename ; 첨부파일 절대 경로(파일명 포함)

    	String host = "smtp.cafe24.com";
        String usid = "ve@pmsc.co.kr";
        String pswd = "pms0070";
        
        String from = "ve@pmsc.co.kr";
        String to = toMail;
        
        String content="";
        
        content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
     	content += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
        content += "<head>";
        content += "<title>Smart VE 비밀번호 재설정</title>";
        content += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        content += "</head>";
        content += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
        content += "<table width=\"530\" height=\"570\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse;\">";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/pw_reset_top.jpg\" width=\"530\" height=\"160\" alt=\"Smart VE 비밀번호 재설정이 완료되었습니다\">";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"70\" height=\"100\"></td>";
        content += "		<td width=\"390\" height=\"100\" style=\"font-size:12px; line-height:1.6; vertical-align:top;\">";
        content += "			" + userName +"(" + toMail + ") <br /> 의 계정 비밀번호를 재설정 하시려면 <br /> 아래의 버튼을";
        content += "			클릭해 주세요.<br /> 만약 비밀번호 재설정을 원치 않으신다면 <br />이 메일을";
        content += "			무시하셔도 좋습니다.";
        content += "		</td>";
        content += "		<td width=\"70\" height=\"100\"></td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"50\">";
        //content += "			<a href=\"http://localhost:8080/smartve/change.do?action=change&id="+toMail+"\"><img src=\"images/pw_reset_btn.jpg\" width=\"205\" height=\"50\" alt=\"비밀번호재설정\"></a>";
        content += "			<a href=\"http://121.78.122.210:8080/smartve/change.do?action=change&id="+toMail+"\"><img src=\"images/pw_reset_btn.jpg\" width=\"205\" height=\"50\" alt=\"비밀번호재설정\"></a>";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"35\">";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td width=\"70\" height=\"58\" ";
        content += "		</td>";
        content += "		<td width=\"390\" height=\"100\" style=\"font-size:12px; line-height:1.6; vertical-align:top;\">";
        content += "		위의 버튼이 작동하지 않는다면 아래 URL을 복사해서 주소창에<br />";
        content += "		붙여 넣기 하세요.<br />";
        content += "		</td>";
        content += "		<td width=\"70\" height=\"50\" ";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"70\" height=\"58\" ";
        content += "		</td>";
        content += "		<td width=\"390\" height=\"58\" align=\"center\" background=\"http://121.78.122.210:8080/smartve/resources/images/pw_reset_bg.jpg\" style=\"font-size:12px; line-height:1.6;\">";
//        content += "		http://localhost:8080/smartve/change.do?action=change&id="+toMail+"" ;
        content += "		http://121.78.122.210:8080/smartve/change.do?action=change&id="+toMail+"" ;
        content += "		</td>";
        content += "		<td width=\"70\" height=\"58\" ";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"22\">";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_07.jpg\" width=\"530\" height=\"95\" alt=\"Smart VE\">";
        content += "		</td>";
        content += " 	</tr>";
        content += "</table>";
        content += "</body>";
        content += "</html>";
        
        
        
        String title = "[PMSC Smart VE] 안녕하세요. " + userName +"(" + toMail + ") 님의 SMART VE 비밀번호 재설정 합니다.";

    	try{
	    	Properties props = new Properties();
	    	props.put("mail.transport.protocol", "smtp");
	    	props.put("mail.smtp.starttls.enable","true");
	    	props.put("mail.smtp.host", host);
	    	props.put("mail.smtp.auth", "true");
	    	Authenticator authenticator = new SMTPAuthenticator(usid, pswd);

	    	// use mail address from HTML form for from address
	    	props.put("mail.from", from);
	    	Session session = Session.getInstance(props, authenticator);

	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(from));
	    	msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	    	msg.setSubject(title);
	    	msg.setSentDate(new Date());
	    	
	    	// Content is stored in a MIME multi-part message
	    	// with one body part
	    	MimeBodyPart mbp = new MimeBodyPart();
	    	//mbp.setText(content);
	    	mbp.setContent(content, "text/html;charset=utf-8");
	    	Multipart mp = new MimeMultipart();
	    	mp.addBodyPart(mbp);
	    	msg.setContent(mp, "text/html;charset=utf-8");

	    	Transport.send(msg);


        }
        catch(Exception e){
        	e.printStackTrace();
        }

    	return "succ";
    }
    
    public String sendExcel(String toMail, String idx) throws Exception {


    	String host = "smtp.cafe24.com";
        String usid = "ve@pmsc.co.kr";
        String pswd = "pms0070";
        
        String from = "ve@pmsc.co.kr";
        String to = toMail;
        
        String content="";
        
        content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
     	content += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
        content += "<head>";
        content += "<title>Smart VE 기능목록</title>";
        content += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        content += "</head>";
        content += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
        content += "<table width=\"530\" height=\"550\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse;\">";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "		<td width=\"380\" height=\"100\" leftmargin=\"75\" style=\"font-size:12px; line-height:1.6;\">";

        content += "			아래 '엑셀자료받기' 텍스트를 클릭해 주세요.";
        content += "		</td>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"180\">";
        //content += "			<a href=\"http://localhost:8080/smartve/mailok.do?action=mailok&id="+toMail+"\"><img src=\"images/auth_04.jpg\" width=\"205\" height=\"50\" alt=\"가입완료\"></a>";
        content += "			<a href=\"http://121.78.122.210:8080/smartve/excelDown.do?idx="+idx+"\">엑셀자료받기</a>";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_07.jpg\" width=\"530\" height=\"95\" alt=\"SMART VE\">";
        content += "		</td>";
        content += "	</tr>";
        content += "</table>";
        content += "</body>";
        content += "</html>";
        
        
        
        String title = "[PMSC Smart VE] 기능목록입니다.";

    	try{
	    	Properties props = new Properties();
	    	props.put("mail.transport.protocol", "smtp");
	    	props.put("mail.smtp.starttls.enable","true");
	    	props.put("mail.smtp.host", host);
	    	props.put("mail.smtp.auth", "true");
	    	Authenticator authenticator = new SMTPAuthenticator(usid, pswd);

	    	// use mail address from HTML form for from address
	    	props.put("mail.from", from);
	    	Session session = Session.getInstance(props, authenticator);

	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(from));
	    	msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	    	msg.setSubject(title);
	    	msg.setSentDate(new Date());
	    	
	    	// Content is stored in a MIME multi-part message
	    	// with one body part
	    	MimeBodyPart mbp = new MimeBodyPart();
	    	//mbp.setText(content);
	    	mbp.setContent(content, "text/html;charset=utf-8");
	    	Multipart mp = new MimeMultipart();
	    	mp.addBodyPart(mbp);
	    	msg.setContent(mp, "text/html;charset=utf-8");

	    	Transport.send(msg);


        }
        catch(Exception e){
        	e.printStackTrace();
        }

    	return "succ";
    }

}
